package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.packet.DiscoverItems.Item;

public class GatewayManager
{
  private static Map<Connection, GatewayManager> instances = new HashMap();
  private ServiceDiscoveryManager sdManager;
  private Map<String, Gateway> localGateways = new HashMap();
  private Map<String, Gateway> nonLocalGateways = new HashMap();
  private Map<String, Gateway> gateways = new HashMap();
  private Connection connection;
  private Roster roster;

  private GatewayManager()
  {
  }

  private GatewayManager(Connection paramConnection)
    throws XMPPException
  {
    this.connection = paramConnection;
    this.roster = paramConnection.getRoster();
    this.sdManager = ServiceDiscoveryManager.getInstanceFor(paramConnection);
  }

  private void loadLocalGateways()
    throws XMPPException
  {
    DiscoverItems localDiscoverItems = this.sdManager.discoverItems(this.connection.getHost());
    Iterator localIterator = localDiscoverItems.getItems();
    while (localIterator.hasNext())
    {
      String str = ((DiscoverItems.Item)localIterator.next()).getEntityID();
      discoverGateway(str);
    }
  }

  private void discoverGateway(String paramString)
    throws XMPPException
  {
    DiscoverInfo localDiscoverInfo = this.sdManager.discoverInfo(paramString);
    Iterator localIterator = localDiscoverInfo.getIdentities();
    while (localIterator.hasNext())
    {
      DiscoverInfo.Identity localIdentity = (DiscoverInfo.Identity)localIterator.next();
      String str = localIdentity.getCategory();
      if (str.toLowerCase().equals("gateway"))
      {
        this.gateways.put(paramString, new Gateway(this.connection, paramString));
        if (paramString.contains(this.connection.getHost()))
        {
          this.localGateways.put(paramString, new Gateway(this.connection, paramString, localDiscoverInfo, localIdentity));
          break;
        }
        this.nonLocalGateways.put(paramString, new Gateway(this.connection, paramString, localDiscoverInfo, localIdentity));
        break;
      }
    }
  }

  private void loadNonLocalGateways()
    throws XMPPException
  {
    if (this.roster != null)
    {
      Iterator localIterator = this.roster.getEntries().iterator();
      while (localIterator.hasNext())
      {
        RosterEntry localRosterEntry = (RosterEntry)localIterator.next();
        if ((localRosterEntry.getUser().equalsIgnoreCase(StringUtils.parseServer(localRosterEntry.getUser()))) && (!localRosterEntry.getUser().contains(this.connection.getHost())))
          discoverGateway(localRosterEntry.getUser());
      }
    }
  }

  public GatewayManager getInstanceFor(Connection paramConnection)
    throws XMPPException
  {
    synchronized (instances)
    {
      if (instances.containsKey(paramConnection))
        return (GatewayManager)instances.get(paramConnection);
      GatewayManager localGatewayManager = new GatewayManager(paramConnection);
      instances.put(paramConnection, localGatewayManager);
      return localGatewayManager;
    }
  }

  public List<Gateway> getLocalGateways()
    throws XMPPException
  {
    if (this.localGateways.size() == 0)
      loadLocalGateways();
    return new ArrayList(this.localGateways.values());
  }

  public List<Gateway> getNonLocalGateways()
    throws XMPPException
  {
    if (this.nonLocalGateways.size() == 0)
      loadNonLocalGateways();
    return new ArrayList(this.nonLocalGateways.values());
  }

  public void refreshNonLocalGateways()
    throws XMPPException
  {
    loadNonLocalGateways();
  }

  public Gateway getGateway(String paramString)
  {
    if (this.localGateways.containsKey(paramString))
      return (Gateway)this.localGateways.get(paramString);
    if (this.nonLocalGateways.containsKey(paramString))
      return (Gateway)this.nonLocalGateways.get(paramString);
    if (this.gateways.containsKey(paramString))
      return (Gateway)this.gateways.get(paramString);
    Gateway localGateway = new Gateway(this.connection, paramString);
    if (paramString.contains(this.connection.getHost()))
      this.localGateways.put(paramString, localGateway);
    else
      this.nonLocalGateways.put(paramString, localGateway);
    this.gateways.put(paramString, localGateway);
    return localGateway;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.GatewayManager
 * JD-Core Version:    0.6.1
 */